//
//  UIImage+Extension.swift
//  HinsLive
//
//  Created by haoxian on 2019/4/17.
//  Copyright © 2019 Hins. All rights reserved.
//

import UIKit

extension UIImage {
    static func image(size: CGSize, color: UIColor) -> UIImage? {
        guard size.width > 0, size.height > 0 else { return nil }
        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
        defer { UIGraphicsEndImageContext() }
        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        context.setFillColor(color.cgColor)
        context.fill(rect)
        return UIGraphicsGetImageFromCurrentImageContext()
    }
    
    func makeCircle(with radius: CGFloat) -> UIImage {
        guard radius > 0 else { return self }
        UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
        defer { UIGraphicsEndImageContext() }
        guard let context = UIGraphicsGetCurrentContext() else { return self }
        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        let path = UIBezierPath(roundedRect: rect, cornerRadius: radius)
        context.addPath(path.cgPath)
        context.clip()
        draw(in: rect)
        guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return self }
        return image
    }
    
    func thumbnail(width: CGFloat = 720, height: CGFloat = 1280) -> UIImage {
        guard width > 0, height > 0 else { return self }
        if size.width <= width, size.height <= height { return self }
        var size = self.size
        let wScale = width / size.width
        let hScale = height / size.height
        if wScale < hScale {
            size = CGSize(width: width, height: wScale * size.height)
        } else {
            size = CGSize(width: hScale * size.width, height: height)
        }
        return autoreleasepool {
            UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
            defer { UIGraphicsEndImageContext() }
            self.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
            guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return self }
            return image
        }
    }
    
    func thumbnailImageData(width: CGFloat = 720, height: CGFloat = 1280, maxByteCount: Int = 300) -> Data? {
        let image = thumbnail(width: width, height: height)
        var compression: CGFloat = 1
        guard var thumbnailData = image.jpegData(compressionQuality: compression) else { return nil }
        var max: CGFloat = 1, min: CGFloat = 0
        for _ in 0..<6 {
            compression = (max + min) / 2
            guard let data = jpegData(compressionQuality: compression) else { return nil }
            thumbnailData = data
            if Double(thumbnailData.count) < Double(maxByteCount) * 0.9 {
                min = compression
            } else if data.count > maxByteCount {
                max = compression
            } else {
                break
            }
        }
        return thumbnailData
    }
}
